<?php
	// On prolonge la session
	session_start();
	$id_anim='';
	$date_n='';
	$code='';
$lecture_tout =0;
$droit_saisie =0;
	require_once('../../classes/database.php');
	$idconnect=Database::getInstance();

	//vérification des droits de l'utilisateur

		// On teste si la variable de session existe et contient une valeur
		if(empty($_SESSION['id_anim'])){
			// Si inexistante ou nulle, on redirige vers la page de saisie du code
			header('Location: ../recherche.php');
			exit();
		}
		else{
			$id_anim=$_SESSION['id_anim'];
			$code=$_SESSION['code'];
			$date_n=$_SESSION['date_n'];
		}
	
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="../../style.css" />
        <?php
			require_once(dirname(__FILE__)."/onglet_animal.php");
			$onglet = affiche_onglets($_SESSION['membre_id']);
		?>
		<!--[if lt IE 9]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <!--[if lte IE 7]>
        <link rel="stylesheet" href="../../style_ie.css" />
        <![endif]-->
        <title>Base de données microcèbes</title>

          <script type="text/javascript" src="../../js/oXHR.js"></script>
        <script>
			function request(oSelect){
				//récupération des informations sur le test donné
				var value = oSelect.options[oSelect.selectedIndex].text;
				var elt = document.getElementById("detail");
				var id = <?php echo $id_anim; ?>;
				var date="<?php echo $date_n;?>";

				if(value!="none"){
					elt.style.display="inline";
					var var1 = encodeURIComponent(value);
					var xhr = getXMLHttpRequest();

					xhr.onreadystatechange = function() {
						if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
							readData(xhr.responseXML);
						}
					};

					xhr.open("POST", "get_mesures.php", true);
					xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					xhr.send("type_test=" + var1+"&id=" + id+"&date_n="+date);
				}
				else{
					elt.style.display="none";
				}
			}
			function readData(oData){
				// création des champs à partir des données du test récupérées dans la base de données
				var nodes   = oData.getElementsByTagName("item");
				var oSelect = document.getElementById("detail");
				var oOption, oText, oInner, oInner2;
				oSelect.innerHTML = "";

				for (var i=0, c=nodes.length; i<c; i++) {
					if (nodes[i].getAttribute("id") == "Date du test") {
						h1 = document.createElement("h1");
						oInner = document.createTextNode("Test ("+ nodes[i].getAttribute("name")+ ") ");
						h1.appendChild(oInner);

						detailLink = document.createElement("button");
						detailLink.setAttribute("onclick", "displayTest('test_"+i+"');");
						detailLink.appendChild(document.createTextNode("Détail"));
						h1.appendChild(detailLink);
						
						oSelect.appendChild(h1);

						paragraph = document.createElement("p");
						paragraph.setAttribute("style", "display:none;");
						paragraph.setAttribute("id", "test_"+i);
					} else {
						oOption = document.createElement("label");
						oText = document.createElement("strong");
						oInner2 = document.createTextNode(nodes[i].getAttribute("id")+": ");
						oInner  = document.createTextNode(nodes[i].getAttribute("name"));
						oSpace = document.createElement("br");
	
						oText.appendChild(oInner2);
						oOption.appendChild(oText);
						oOption.appendChild(oInner);
						paragraph.appendChild(oOption);
						paragraph.appendChild(oSpace);
						oSelect.appendChild(paragraph);
						
					}
				}
			}

			function displayTest(id){
				//affichage du détail du test
				var elt = document.getElementById(id);
				if(elt.style.display=="inline-block"){
					elt.style.display="none";
				}
				else{
					elt.style.display="inline-block";
				}
			}
        </script>
    </head>

    <body>
		<?php include((dirname(dirname(dirname(__FILE__))))."/menu.php"); ?>
		<?php include((dirname(dirname(__FILE__)))."/calcul_age.php"); ?>

		<section id="main">
			<header>
				<h1>Recherche - Tests biologiques animal <?php echo $code;?></h1>
			</header>
			<?php include((dirname(dirname(__FILE__)))."/menu_recherche.php"); ?>
			<section id="animal">
				<?php
					echo $onglet;
				?>

				<section id="contenu">

						<?php
							 //récupération du nom des tests biologiques
							$query = pg_query($idconnect,"SELECT DISTINCT unite_mesure FROM type_test,test,resultat_test
							WHERE label='Test biologique' AND test.id_test=resultat_test.id_test
							AND resultat_test.id_animal='".$id_anim."' AND test.id_type_test=type_test.id_type_test
							ORDER BY unite_mesure");

							$int = pg_num_rows($query);
							if($int==0){echo("<p><label>Pas d'informations disponibles</label></p>");}
							else{
								echo "<h1>Choississez un test:</h1>";
								echo "<select id=\"type_test\" onchange=\"request(this)\">";
								echo "<option value=\"none\">Sélection</option>";
								$i=0;
								while ($back = pg_fetch_assoc($query)) {
									echo "<option id='".$i."'>".$back["unite_mesure"]."</option>";
									$i++;
								}
								echo "</select>";
							}
						?>
					<p id="detail"></p>
				</section>
			</section>

		</section>
	</body>
</html>
